function withData(param) {
  console.log('withData====>')
  return param < 10 ? '0' + param : '' + param;
}

function getLoopArray(start, end) {
  console.log('getLoopArray====>')
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    array.push(withData(i));
  }
  return array;
}

function getMonthDay(year, month) {
  console.log('getMonthDay==month==>', month)
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
    array = null;

  /*   switch (month) {
      case '01月':
      case '03月':
      case '05月':
      case '07月':
      case '08月':
      case '10月':
      case '12月':
        array = getLoopArray(1, 31)
        break;
      case '04月':
      case '06月':
      case '09月':
      case '11月':
        array = getLoopArray(1, 30)
        break;
      case '02月':
        array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
        break;
      default:
        array = '月份格式不正确，请重新输入！'
    } */

  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      array = getLoopArray(1, 31)
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      array = getLoopArray(1, 30)
      break;
    case '02':
      array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
      break;
    default:
      array = '月份格式不正确，请重新输入！'
  }

  return array;

}

function getNewDateArry() {
  console.log('getNewDateArry====>')
  // 当前时间的处理
  var newDate = new Date();
  var year = withData(newDate.getFullYear()), //年
    mont = withData(newDate.getMonth() + 1), //月
    date = withData(newDate.getDate()), //日
    hour = withData(newDate.getHours()), //时
    minu = withData(newDate.getMinutes()), //分
    seco = withData(newDate.getSeconds()); //秒

  console.log('当前时间year=====>', year)
  console.log('当前时间mont=====>', mont)
  console.log('当前时间date=====>', date)
  console.log('当前时间hour=====>', hour)

  return [year, mont, date, hour, minu, seco];
}

function dateTimePicker(startYear, endYear, date) {
  console.log('dateTimePicker====>')
  // 返回默认显示的数组和联动数组的声明
  var dateTime = [],
    dateTimeArray = [
      [],
      [],
      [],
      []
    ];
  var start = startYear || 1978;
  var end = endYear || 2100;

  // 默认开始显示数据
  var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();


  console.log('defaultDate====>', defaultDate)

  // 处理联动列表数据
  /*年月日 时分秒*/


  let dateYears = getLoopArray(start, end);
  let ArrYears = [];

  let dateMonth = getLoopArray(1, 12);
  let ArrMonth = [];

  let dateDay = getMonthDay(defaultDate[0], defaultDate[1]);
  let ArrDay = [];

  let dateHours = getLoopArray(0, 23);
  let ArrHours = [];


  dateYears.forEach((v) => {
    ArrYears.push(v + '年')
  });
  dateMonth.forEach((v) => {
    ArrMonth.push(v + '月')
  });
  dateDay.forEach((v) => {
    ArrDay.push(v + '日')
  });
  dateHours.forEach((v) => {
    ArrHours.push(v + '时')
  });


  console.log('ArrDay====>', ArrDay)




  dateTimeArray[0] = getLoopArray(start, end); //年
  dateTimeArray[1] = getLoopArray(1, 12); //月
  dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]); //日
  dateTimeArray[3] = getLoopArray(0, 23); // 时  


  /*   dateTimeArray[0] = ArrYears; //年
    dateTimeArray[1] = ArrMonth; //月
    dateTimeArray[2] = ArrDay; //日
    dateTimeArray[3] = ArrHours; // 时  */


  dateTimeArray.forEach((current, index) => {
    console.log('current==>', current)
    console.log('index==>', index)
    dateTime.push(current.indexOf(defaultDate[index]));
  });

  return {
    dateTimeArray: dateTimeArray,
    dateTime: dateTime
  }
}
module.exports = {
  dateTimePicker: dateTimePicker,
  getMonthDay: getMonthDay
}